package com.cmas.manage.controller;

import java.util.List;
import java.util.Arrays;

import com.cmas.common.annotation.Log;
import com.cmas.common.core.controller.BaseController;
import com.cmas.common.core.domain.AjaxResult;
import com.cmas.common.core.page.TableDataInfo;
import com.cmas.common.enums.BusinessType;
import com.cmas.common.utils.poi.ExcelUtil;
import com.cmas.manage.domain.CApply;
import com.cmas.manage.domain.Community;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cmas.manage.vo.CommunityVo;
import com.cmas.manage.bo.CommunityQueryBo;
import com.cmas.manage.bo.CommunityAddBo;
import com.cmas.manage.bo.CommunityEditBo;
import com.cmas.manage.service.ICommunityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 社团Controller
 * 
 * @author cmas
 * @date 2021-05-03
 */
@Api(value = "社团控制器", tags = {"社团管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/manage/community")
public class CommunityController extends BaseController {

    private final ICommunityService iCommunityService;

    /**
     * 查询社团列表
     */
    @ApiOperation("查询社团列表")
    @PreAuthorize("@ss.hasPermi('manage:community:list')")
    @GetMapping("/list")
    public TableDataInfo<CommunityVo> list(CommunityQueryBo bo) {
        startPage();
        List<CommunityVo> list = iCommunityService.queryList(bo);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('manage:community:list')")
    @GetMapping("/listVo")
    public TableDataInfo<Community> listVo(Community community) {
        startPage();
        List<Community> list = iCommunityService.listVo(community);
        return getDataTable(list);
    }

    /**
     * 导出社团列表
     */
    @ApiOperation("导出社团列表")
    @PreAuthorize("@ss.hasPermi('manage:community:export')")
    @Log(title = "社团", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<CommunityVo> export(CommunityQueryBo bo) {
        List<CommunityVo> list = iCommunityService.queryList(bo);
        ExcelUtil<CommunityVo> util = new ExcelUtil<CommunityVo>(CommunityVo.class);
        return util.exportExcel(list, "社团");
    }

    /**
     * 获取社团详细信息
     */
    @ApiOperation("获取社团详细信息")
    @PreAuthorize("@ss.hasPermi('manage:community:query')")
    @GetMapping("/{id}")
    public AjaxResult<CommunityVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCommunityService.queryById(id));
    }

    /**
     * 根据社长获取社团
     * @param userId
     * @return
     */
    @GetMapping("/getCommunityByUser/{userId}")
    public AjaxResult<List<Community>> getCommunityByUser(@PathVariable("userId" ) Long userId) {
        return AjaxResult.success(iCommunityService.getCommunityByUser(userId));
    }


    /**
     * 获取社团申请待审批信息
     * @param userId
     * @return
     */
    @GetMapping("/getWaitApprovalByUser/{userId}")
    public TableDataInfo<CApply> getWaitApprovalByUser(@PathVariable("userId" ) Long userId) {
        startPage();
        List<CApply> list = iCommunityService.getWaitApprovalByUser(userId);
        return getDataTable(list);
    }

    @GetMapping("/getAgreeByUser/{userId}")
    public TableDataInfo<CApply> getAgreeByUser(@PathVariable("userId" ) Long userId) {
        startPage();
        List<CApply> list = iCommunityService.getAgreeByUser(userId);
        return getDataTable(list);
    }

    @GetMapping("/getRefuseByUser/{userId}")
    public TableDataInfo<CApply> getRefuseByUser(@PathVariable("userId" ) Long userId) {
        startPage();
        List<CApply> list = iCommunityService.getRefuseByUser(userId);
        return getDataTable(list);
    }



    /**
     * 新增社团
     */
    @ApiOperation("新增社团")
    @PreAuthorize("@ss.hasPermi('manage:community:add')")
    @Log(title = "社团", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody CommunityAddBo bo) {
        return toAjax(iCommunityService.insertByAddBo(bo) ? 1 : 0);
    }


    @Log(title = "申请加入社团", businessType = BusinessType.INSERT)
    @PostMapping("/joinCommunity")
    public AjaxResult<Integer> joinCommunity(@RequestBody CApply cApply) {
        return AjaxResult.success(iCommunityService.joinCommunity(cApply));
    }

    /**
     * 修改社团
     */
    @ApiOperation("修改社团")
    @PreAuthorize("@ss.hasPermi('manage:community:edit')")
    @Log(title = "社团", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody CommunityEditBo bo) {
        return toAjax(iCommunityService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除社团
     */
    @ApiOperation("删除社团")
    @PreAuthorize("@ss.hasPermi('manage:community:remove')")
    @Log(title = "社团" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@PathVariable Long[] ids) {
        return toAjax(iCommunityService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    @ApiOperation("入团申请同意审批")
    @PreAuthorize("@ss.hasPermi('manage:approval:agree')")
    @PostMapping("/agreeJoinCmt/{ids}")
    public AjaxResult<Void> agreeJoinCmt(@PathVariable Long[] ids) {
        return toAjax(iCommunityService.agreeJoinCmt(Arrays.asList(ids)) );
    }

    @ApiOperation("入团申请拒绝审批")
    @PreAuthorize("@ss.hasPermi('manage:approval:refuse')")
    @PostMapping("/refuseJoinCmt/{ids}")
    public AjaxResult<Void> refuseJoinCmt(@PathVariable Long[] ids) {
        return toAjax(iCommunityService.refuseJoinCmt(Arrays.asList(ids)) );
    }
}
